<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    private $_acl = null;
    private $_auth = null;

    protected function _initBaseUrl() {
        $this->bootstrap("frontController");
        $front = $this->getResource("frontController");
        $request = new Zend_Controller_Request_Http();
        $front->setRequest($request);
    }

    public function _initView() {

        $view = new Zend_View();

        $view->doctype('HTML5'); // XHTML1_STRICT
        $view->headTitle('Cabinet')->setSeparator(' - ')->append('Cabinets.com');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=utf-8');

        /*
          $view->headLink()->prependStylesheet($view->baseUrl() . '/css/twistter/bootstrap.css')
          ->appendStylesheet($view->baseUrl() . '/css/twistter/docs/assets/css/docs.css')
          ->appendStylesheet($view->baseUrl() . '/css/twister-custom.css');

         */

        $view->headLink()->prependStylesheet($view->baseUrl() . '/css/bootstrap.css')
                //->appendStylesheet($view->baseUrl() . '/css/docs/assets/css/docs.css')
                ->appendStylesheet($view->baseUrl() . '/css/twister-custom.css');
        //$view->headLink()->
        // $view->headScript()->appendFile('/js/bootstrap-dropdown.js','text/javascript', array('conditional' => 'lt IE 7');
       // $view->headScript()->appendFile($view->baseUrl() . '/js/bootstrap-dropdown.js');
        $view->headScript()->appendFile($view->baseUrl() . '/js/bootstrap.min.js');

        $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
        $view->jQuery()->enable();
        $view->jQuery()->uiEnable();


//<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/humanity/jquery-ui.css">
//<script charset="utf-8" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
//<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/i18n/jquery-ui-i18n.min.js"></script>
//<script charset="utf-8" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script>

        $view->jQuery()->setVersion('1.7');
        $view->jQuery()->setUiVersion('1.8');
        $view->jQuery()->addStyleSheet($view->baseUrl() . '/js/jquery/themes/ui-lightness/jquery-ui-1.8.16.custom.css');
        $view->jQuery()->addJavascriptFile($view->baseUrl() . '/js/jQuery/ui/i18n/jquery-ui-i18n.js');

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);

        return $view;
    }

    protected function _initTranslate() {

        $locale = new Zend_Locale('fr_FR'); //'fr_FR'
        Zend_Registry::set('locale', $locale);
        $translate = new Zend_Translate('array', '../languages/fr/fr.php', 'fr');
        $translate->setLocale('fr_FR');
        Zend_Registry::set('Zend_Translate', $translate);
        return $translate;
    }

    protected function _initAcl() {

        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $this->_acl = new Cab_Acl_Acl();
        $this->_auth = Zend_Auth::getInstance();

        if (!$this->_auth->hasIdentity()) {
            $this->_auth->getStorage()->read()->role = 'guest';
        }

// $auth est une référence vers Zend_Auth (getInstance())
        $frontController->registerPlugin(new Cab_Controller_Plugin_Acl($this->_acl, $this->_auth));
    }

    protected function _initNavigation() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        $navConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
        $navigation = new Zend_Navigation($navConfig);

// Zend_Debug::dump($this->_auth->getStorage()->read()->role);
        $view->navigation($navigation)->setAcl($this->_acl)
                ->setRole($this->_auth->getStorage()->read()->role);
    }

    protected function _initZFDebug() {

        if ($this->hasOption('zfdebug')) {
            $zfdebug = $this->getOption('zfdebug');

            if ($zfdebug['enabled'] == 1) {

                $options = array(
                    'jquery_path' => '', /* desactive le lien vers jQuery */
                    'plugins' => array('Variables',
                        'File' => array('base_path' => realpath(APPLICATION_PATH . '/../')),
//'Memory',
                        'Html',
//'Time',
                        'Registry',
                        'Exception'));

# Instantiate the database adapter and setup the plugin.
# Alternatively just add the plugin like above and rely on the autodiscovery feature.
                if ($this->hasPluginResource('db')) {
                    $this->bootstrap('db');
                    $db = $this->getPluginResource('db')->getDbAdapter();
                    $options['plugins']['Database']['adapter'] = $db;
                }

# Setup the cache plugin
                if ($this->hasPluginResource('cachemanager')) {
                    $this->bootstrap('cachemanager');
                    $cache = $this->getPluginResource('cachemanager')->getCacheManager();

                    $options['plugins']['Cache']['backend'] = $cache->getCache('database')->getBackend();

# set up database meta data cache
                    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache->getCache('database'));
                }

# Setup the cache plugin
                if ($this->hasPluginResource('cache')) {
                    $this->bootstrap('cache');
                    $cache = $this - getPluginResource('cache')->getDbAdapter();
                    $options['plugins']['Cache']['backend'] = $cache->getBackend();
                }

                $debug = new ZFDebug_Controller_Plugin_Debug($options);

                $this->bootstrap('frontController');
                $frontController = $this->getResource('frontController');
                $frontController->registerPlugin($debug);
            }
        }
    }

}
